using imitateMacQQ.WebApi.Repository.EFCore.Model;

namespace imitateMacQQ.WebApi.Domain.FirstCode.schema;

/// <summary>
/// FIRST_CODE MODE
/// </summary>
public static class Admin
{
    /// <summary>
    /// 链接配置与初始化
    /// </summary>
    /// <param name="configuration">JSON_READER</param>
    public static async Task Init(IConfiguration configuration)
    {
        var config = configuration.GetSection("Sql:Default");
        Usr = config.GetSection("Usr").Value ?? "NULL";
        Catalog = config.GetSection("Catalog").Value ?? "NULL";
        Port = config.GetSection("Port").Value ?? "NUll";
        Pwd = config.GetSection("Pwd").Value ?? "NULL";
       
        var webRootPath = Path.Combine(Directory.GetCurrentDirectory(),
            "StaticFiles/");
        await using var db = new DataDbContext();
        try
        {
            var admin = db.Users.FirstOrDefault(e => e.Account == "1327916255");
            Console.WriteLine("SQL_Config:" + Usr + "----" + Catalog + "----" + Port);
            Console.WriteLine($"WEBSERVICE:BASE_FILE{webRootPath} START!");
            Console.WriteLine(@"__   _______ ____          ");
            Console.WriteLine(@"\ \ / /_   _/ __ \   /\    ");
            Console.WriteLine(@" \ V /  | || |  | | /  \   ");
            Console.WriteLine(@"  > <   | || |  | |/ /\ \  ");
            Console.WriteLine(@" / . \ _| || |__| / ____ \ ");
            Console.WriteLine(@"/_/ \_\_____\____/_/    \_\");
            Console.WriteLine(@$"{admin?.Id}_________ ____ ");
        }
        catch
        {
            await db.Database.EnsureCreatedAsync();
            db.Users.Add(new User
            {
                Account = "1327916255",
                Password = "123456789",
                IsVip = true,
                UserName = "XIOA",
                Sex = "GOD",
                Intro = "",
                Header = "",
                State = 1,
                CreateDateTime = DateTime.Now,
                UseDateTime = "0",
                Phone = "13279162555"
            });
            await db.SaveChangesAsync();
            Console.WriteLine("FIRST_CODE_START!");
            await Init(configuration);
        }
    }

    public static string? Usr { get; private set; }
    public static string? Catalog { get; private set; }
    public static string? Port { get; private set; }
    public static string? Pwd { get; private set; }
}